//
//  MyCloudDetailViewModel.swift
//  EyeVisionForPatients
//
//  Created by Mr.Lin. on 2020/3/15.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

/*
{
  "data" : {
    "doctorId" : "1158208468874235906",
    "oneselfState" : "患者自述",
    "patientName" : "林蔚",
    "pastHistory" : "既往史",
    "patientAge" : 30,
    "doctorUserId" : "1158208468815515650",
    "patientUserId" : "1238698426779271169",
    "patientNoMsgId" : "",
    "diagnoseId" : "1239457964625154049",
    "doctorNoMsgId" : "",
    "isPrescription" : 0,
    "doctorName" : "姜小涵",
    "allergyHistory" : "过敏史",
    "resultList" : [
        "ext" : "jpg", //后缀
        "shareUrl" : "",
        "url" : "abc", //原图
        "shareThumbnailUrl" : "",
        "thumbnailUrl" : "abc" //缩略图
    ],
    "jobTitle" : "住院医师",
    "patientUnreadCount" : 0,
    "doctorUnreadCount" : 0,
    "patientSex" : "男",
    "flag" : 301,
    "patientId" : "1238698426871545857"
  },
  "message" : "操作成功",
  "code" : 200
}
 */

struct MyCloudReadMeImgModel: HandyJSON {
    //图片ID
    var resourceId: String?
    //后缀
    var ext: String?
    //原图
    var url: String?
    //缩略图
    var thumbnailUrl: String?
}

struct MyCloudReadMeImgViewModel {
    let model: MyCloudReadMeImgModel
    init(model: MyCloudReadMeImgModel) {
        self.model = model
        self.resourceId = model.resourceId ?? ""
        self.ext = model.ext ?? ""
        self.url = model.url ?? ""
        self.thumbnailUrl = model.thumbnailUrl ?? ""
    }
    var resourceId: String
    var ext: String
    var url: String
    var thumbnailUrl: String
}

struct MyCloudReadMeDetailModel: HandyJSON {
    var orderStatus: Int = -1
    var orderNumber: String?
    var orderId: String?
    var remainPayTime: String?
    var hospitalName: String?
    var deptName: String?
    var doctorName: String?
    var canDelete: Bool = false
    var canCancel: Bool = false
    var patientName: String?
    var patientId: String?
    var orderAmount: String?
    var jobTitleName: String?
    var scheduleDate: String?
    //患者自述症状
    var oneselfState: String?
    //过敏史
    var allergyHistory: String?
    //既往史
    var pastHistory: String?
    //图片数组
    var resultList: [MyCloudReadMeImgModel]?
}

struct MyCloudReadMeDetailViewModel {
    init(model: MyCloudReadMeDetailModel) {
        self.model = model
        self.allergyHistory = model.allergyHistory ?? ""
        self.pastHistory = model.pastHistory ?? ""
        self.oneselfState = model.oneselfState ?? ""
        self.resultList = model.resultList ?? []
    }
    let model: MyCloudReadMeDetailModel
    var allergyHistory: String
    var pastHistory: String
    var oneselfState: String
    var resultList: [MyCloudReadMeImgModel]
    var isAllergyHistory: Bool { return !allergyHistory.isEmpty }
    var numOfRows: Int { return resultList.count }
    func viewModel(for indexPath: IndexPath) -> MyCloudReadMeImgViewModel {
        return MyCloudReadMeImgViewModel(model: resultList[indexPath.row])
    }
}

extension MyCloudReadMeDetailViewModel {
    /// 获取云门诊患者自述详情
    static func getCloudPatientReadMeDetail(diagnoseId: String, success: @escaping (_ model: MyCloudReadMeDetailModel) -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["diagnoseId": diagnoseId]
        HttpTool.shared.request(target: ApiManager.cloudPatientReadMeDetail(paras: paras), success: { (json, code) in
            switch code {
            case.failure:
                failure(json["message"].description)
            case.success:
                YSGLog(json)
                if let model = JSONDeserializer<MyCloudReadMeDetailModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍候重试")
                }
            }
        }) { (errorMsg) in
            failure(errorMsg)
        }
    }
}
/*
{
  "message" : "操作成功",
  "data" : {
    "hospitalName" : "",
    "doctorName" : "陈蔚",
    "orderStatus" : "5",
    "regDate" : "2020-03-17",
    "patientId" : "1238698426871545857",
    "deptName" : "干眼门诊",
    "remainPayTime" : "0",
    "patientType" : "1",
    "hos_doc_code" : "",
    "shiftCode" : "am",
    "scheduleDate" : "03-17  星期二",
    "readyFlag" : 0,
    "scheduleTime" : "08:30-08:30",
    "orderId" : "1238699711427477505",
    "jobTitleName" : "主任医生",
    "canDelete" : true,
    "orderNumber" : "20200314133319779271169050",
    "canSignIn" : false,
    "remainTime" : 0,
    "relationId" : "",
    "diagnoseId" : "1238699711339397122",
    "patientName" : "林蔚",
    "orderAmount" : 10000,
    "medicareAmount" : 2000,
    "queueNum" : 0,
    "his_dept_id" : "",
    "canCancel" : false
  },
  "code" : 200
}
 */

struct MyCloudDetailModel: HandyJSON {
    var hospitalName: String?
    var doctorName: String?
    var orderStatus: String?
    var regDate: String?
    var patientId: String?
    var deptName: String?
    var remainPayTime: Int = 0
    var patientType: String?
    var hos_doc_code: String?
    var shiftCode: String?
    var scheduleDate: String?
    var readyFlag: Int = 0
    var scheduleTime: String?
    var orderId: String?
    var jobTitleName: String?
    var canDelete: Bool = false
    var orderNumber: String?
    var canSignIn: Bool = false
    var remainTime: Int = 0
    var relationId: String?
    var diagnoseId: String?
    var patientName: String?
    var orderAmount: String?
    var medicareAmount: Int = 0
    var queueNum: Int = 0
    var his_dept_id: String?
    var canCancel: Bool = false
    var amPm: String?
    
}

struct MyCloudDetailViewModel {
    init(model: MyCloudDetailModel) {
        self.model = model
        self.doctorName = model.doctorName ?? ""
        self.deptAndJobTitle = (model.deptName ?? "") + " " + (model.jobTitleName ?? "")
        self.hospitalName = model.hospitalName ?? ""
        self.scheduleTime = model.scheduleTime ?? ""
        self.scheduleDate = (model.scheduleDate ?? "")
        self.patientName = model.patientName ?? ""
        self.diagnoseId = model.diagnoseId ?? ""
        self.orderNumber = model.orderNumber ?? ""
        self.orderAmount = model.orderAmount ?? ""
        self.shiftCode = model.shiftCode ?? ""
        self.remainPayTime = "\(model.remainPayTime / 60)"
        
        self.payOrderModel.orderNumber = model.orderNumber ?? ""
        self.payOrderModel.diagnoseId = model.diagnoseId ?? ""
        self.payOrderModel.orderId = model.orderId ?? ""
        self.publishViewModel.doctorName = model.doctorName ?? ""
        self.publishViewModel.departmentName = model.deptName ?? ""
        self.publishViewModel.departmentName = model.jobTitleName ?? ""
        self.publishViewModel.reserveTime = (model.scheduleDate ?? "") + " " + (self.amPm)
        self.publishViewModel.scheduleTime = model.scheduleTime ?? ""
    }
    var payOrderModel = PayOrderModel()
    var publishViewModel = PublishViewModel()
    
    let model: MyCloudDetailModel
    var doctorName: String
    var deptAndJobTitle: String
    var hospitalName: String
    var scheduleTime: String // 08:30-08:30
    var scheduleDate: String // 03-17  星期二
    var patientName: String
    var shiftCode: String // am pm
    var amPm: String {
        if shiftCode == "am" {
            return "上午"
        }
        else if shiftCode == "pm" {
            return "下午"
        } else {
            return ""
        }
    }
    var diagnoseId: String
    var orderNumber: String
    var orderAmount: String
    var remainPayTime: String
}


extension MyCloudDetailViewModel {

    /// 获取云门诊订单详情
    static func getCloudOrderDetail(orderId: String, success: @escaping (_ model: MyCloudDetailModel) -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["orderId": orderId]
        HttpTool.shared.request(target: ApiManager.cloudOrderDetail(paras: paras), success: { (json, code) in
            switch code {
            case.failure:
                failure(json["message"].description)
            case.success:
                if let model = JSONDeserializer<MyCloudDetailModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍候重试")
                }
            }
            YSGLog(json)
        }) { (errorMsg) in
            failure(errorMsg)
        }
    }
}
